<template>
	<el-dialog
		:title="titleMap[mode]"
		v-model="visible"
		:width="700"
		destroy-on-close
		append-to-body
		@closed="emits('closed')"
	>
		<el-form
			:model="form"
			:rules="rules"
			ref="dialogForm"
			label-width="80px"
			:style="'el-dialog' === 'el-drawer' ? 'padding:0 20px' : ''"
		>

			<el-form-item label="用户名" prop="username">
				<el-input v-model="form.username" clearable placeholder="请输入"/>
			</el-form-item>

			<el-form-item label="手机" prop="phone">
				<el-input v-model="form.phone" clearable placeholder="请输入"/>
			</el-form-item>

		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script setup>
import {ref, reactive, defineExpose, onMounted, getCurrentInstance} from 'vue'
import {ElMessage} from 'element-plus'
import systemKikiUser from '@/api/apis/system/systemKikiUser'
import systemDict from '@/api/apis/system/dataDict'
import {useRouter} from "vue-router";


const router = useRouter();
const emits = defineEmits(['success', 'closed'])
const {proxy} = getCurrentInstance();

const mode = ref('add')
const treeList = ref([])
const visible = ref(false)
const isSaveing = ref(false)
const dialogForm = ref(null)

const titleMap = reactive({add: '新增kiki一对一', edit: '编辑kiki一对一'})
const dictData = reactive({})
const form = reactive({
	id: '0',
	username: '',
	phone: '',

})
const rules = reactive({})

onMounted(async () => {
	await getDictData()
	if(router.currentRoute.value.query.open) {
		open('add');
	}
})

const getDictData = () => {

}

const open = (type = 'add') => {
	mode.value = type
	visible.value = true

}

const submit = () => {
	dialogForm.value.validate(async (valid) => {
		if (valid) {
			isSaveing.value = true
			const res = mode.value === 'add' ? await systemKikiUser.save(form) : await systemKikiUser.update(form.id, form)
			if (res.success) {
				emits('success', form, mode.value)
				visible.value = false
				ElMessage.success(res.message)
			} else {
				ElMessage.error(res.message)
			}
			isSaveing.value = false
		}
	})
}

const setData = (data) => {
	for (let k in form) {
		if (data[k] || data[k] === 0) {
			form[k] = data[k]
		}
	}
}


defineExpose({
	open, setData
})
</script>
